package com.gton.controller;



import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gton.io.BaseLimitRequest;
import com.gton.io.BaseLimitResponse;
import com.gton.io.Resp;
import com.gton.entity.GpLogin;
import com.gton.service.GpLoginService;
import org.springframework.web.bind.annotation.*;
import com.gton.annotation.SwaggerScanClass;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Arrays;
import java.io.Serializable;
import java.util.List;

/**
 * (GpLogin)表控制层
 *
 * @author 郭童
 * @since 2023-07-07 15:22:25
 */
@RestController
@RequestMapping("gpLogin")
@SwaggerScanClass
public class GpLoginController  {
    /**
     * 服务对象
     */
    @Autowired
    private GpLoginService gpLoginService;

   /**
     * 分页查询数据
     *
     * @param limitRequest 查询实体
     * @return 所有数据
     */
    @PostMapping("/queryLimit")
     public Resp<BaseLimitResponse<GpLogin>> queryPage(@RequestBody BaseLimitRequest<GpLogin> limitRequest) {
        // 分页查询
        IPage<GpLogin> page = this.gpLoginService.queryLimitPage(limitRequest);
        // 封装返回结果集
        BaseLimitResponse<GpLogin> data = BaseLimitResponse.getInstance(page.getRecords(), page.getTotal(), page.getPages(), limitRequest.getPageIndex(), limitRequest.getPageSize());
        return Resp.Ok(data);
    }
   
   /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/queryOne/{id}")
    public Resp<GpLogin> selectOne(@PathVariable("id") Serializable id) {
         return Resp.Ok(this.gpLoginService.getById(id));
    }

   /**
     * 新增数据
     *
     * @param gpLogin 实体对象
     * @return 新增结果
     */
    @PostMapping("/save")
    public Resp<String> insert(@RequestBody GpLogin gpLogin) {
        boolean save = false;
        String executeMsg = null;
        try {
            save = this.gpLoginService.save(gpLogin);
            executeMsg = "新增成功,id 是：" + gpLogin.getId();
        } catch (Exception e) {
            executeMsg = e.getMessage();
        }
        return save ? Resp.Ok(executeMsg) : Resp.error(executeMsg);
    }
    
     /**
     * 批量新增数据
     *
     * @param gpLoginList 实体对象
     * @return 新增结果
     */
    @PostMapping("/saveBatch")
    public Resp<String> insertBatch(@RequestBody List<GpLogin> gpLoginList) {
        Integer save = 0;
        String executeMsg = null;
        try {
            save = this.gpLoginService.saveBatchByEasyBaseMapper(gpLoginList);
            executeMsg = "新增成功=>id 是：" + Arrays.toString(gpLoginList.stream().map(GpLogin::getId).toArray());
        } catch (Exception e) {
            executeMsg = e.getMessage();
        }
        return save > 0 ? Resp.Ok(executeMsg) : Resp.error(executeMsg);
    }


    /**
     * 修改数据
     *
     * @param gpLogin 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    public Resp<String> update(@RequestBody GpLogin gpLogin) {

        boolean update = false;
        String executeMsg = null;
        try {
            update = this.gpLoginService.updateById(gpLogin);
            executeMsg = "修改成功,id 是：" + gpLogin.getId();
        } catch (Exception e) {
            executeMsg = e.getMessage();
        }
        return update ? Resp.Ok(executeMsg) : Resp.error(executeMsg);
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/dels")
    public Resp<String> delete(@RequestParam("idList") List<Long> idList) {
        boolean delNumber = false;
        String executeMsg = null;
        try {
            delNumber = this.gpLoginService.removeByIds(idList);
            executeMsg = "删除成功,ids 是：" + idList;
        } catch (Exception e) {
            executeMsg = e.getMessage();
        }
        return delNumber ? Resp.Ok(executeMsg) : Resp.error(executeMsg);
    }
}

